const usernameEle = document.querySelector('input[name="login-username"]')
const usernameP = document.querySelector('.login-username')
const passwordEle = document.querySelector('input[name="login-password"]')
const passwordP = document.querySelector('.login-password')
const loginBtn = document.querySelector('.loginBtn')
/**
 * 登录
 */
// 用户名非空验证
function checkUsername() {
    if (usernameEle.value == '') {
        usernameP.innerHTML = '用户名不能为空'
        return false
    } else {
        usernameP.innerHTML = ''
        return true
    }
}
// 验证密码组成和是否为空
function checkPassword() {
    // let reg = /[a-zA-Z0-9]{8,12}/
    if (passwordEle.value == '') {
        passwordP.innerHTML = '密码不能为空'
        return false
    }
    /* else if (reg.test(passwordEle.value) == false) {
           passwordP.innerHTML = '请输入正确格式的密码'
           return false
       }  */
    else {
        passwordP.innerHTML = ''
        return true
    }
}
/* // 失去焦点的时候验证
// usernameEle.onblur = function () {
//     checkUsername()
// }
// passwordEle.onblur = function () {
//     checkPassword()
// } */
// 获取登录接口数据
function getLogData(username, password) {
    $.ajax({
        type: 'post',
        url: 'http://api.yuguoxy.com/api/login',
        data: {
            username,
            password,
        },
        success: function (result) {
            if (result.resultCode == 1) {
                location.href = 'index.html'
                let user = localStorage.getItem('USER')
                // JSON.parse(user) ? user = [] : user = JSON.parse(user)
                user = JSON.parse(user) || []
                let thisUser = {
                    name: result.resultInfo.username,
                }
                user.push(thisUser)
                localStorage.setItem('USER', JSON.stringify(user))
            } else {
                alert('用户名密码出错')
            }
        }
    })
}
// 登录
function isUser() {
    if (checkUsername && checkPassword) {
        loginBtn.onclick = function () {
            let username = usernameEle.value
            let password = passwordEle.value
            getLogData(username, password)
        }
    }
}
isUser()

/**
 * 注册
 */
const regesiterUsernameEle = document.querySelector('input[name="register-username"]')
const regesiterUsernameP = document.querySelector('.register-username')
const regesiterPasswordEle = document.querySelector('input[name="register-password"]')
const regesiterImgEle = document.querySelector('input[name="register-img"]')
const regesiterPasswordP = document.querySelector('.register-password')
const registerBtn = document.querySelector('.registerBtn')
// 用户名非空验证
function checkRegisterUsername() {
    if (regesiterUsernameEle.value == '') {
        regesiterUsernameP.innerHTML = '用户名不能为空'
        return false
    } else {
        regesiterUsernameP.innerHTML = ''
        return true
    }
}
// 验证密码组成和是否为空
function checkRegisterPassword() {
    let reg = /[a-zA-Z0-9]{6,12}/
    if (regesiterPasswordEle.value == '') {
        regesiterPasswordP.innerHTML = '密码不能为空'
        return false
    } else if (reg.test(regesiterPasswordEle.value) == false) {
        regesiterPasswordP.innerHTML = '请输入正确格式的密码'
        return false
    } else {
        regesiterPasswordP.innerHTML = ''
        return true
    }
}
// 获取注册接口数据
function getRegisterData(username, password, img) {
    $.ajax({
        type: 'post',
        url: 'http://api.yuguoxy.com/api/sign',
        data: {
            username,
            password,
            headerimg: img,
        },
        success: function (result) {
            console.log(result);
            if (result.resultCode == 1) {
                alert('注册成功')
            } else {
                alert(result.resultInfo)
            }
        }
    })
}
// 注册
function onResigest() {
    if (checkRegisterUsername && checkRegisterPassword) {
        registerBtn.onclick = function () {
            let username = regesiterUsernameEle.value
            let password = regesiterPasswordEle.value
            let img = regesiterImgEle.value
            getRegisterData(username, password, img)
        }
    }
}
onResigest()
// 点击切换登录注册界面
function onTab() {
    $('.message .login-a').click(function () {
        $('.register-form').css('display', 'block')
        $('.login-form').css('display', 'none')
    });
    $('.message .register-a').click(function () {
        $('.register-form').css('display', 'none')
        $('.login-form').css('display', 'block')
    });
}
onTab()